package org.example.soft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.example.soft.Entity.ToDoEventDTO;
import org.example.soft.Entity.UserDTO;
import org.example.soft.common.Result;
import org.example.soft.mapper.ToDoEventDTOmapper;
import org.springframework.web.bind.annotation.*;

@Tag(name = "todoEventController", description = "待办事项管理")
@RestController
@RequestMapping(value = "/todo")
public class ToDoEventDTOController {

    @Resource
    ToDoEventDTOmapper toDoEventDTOmapper;

    @GetMapping("/{id}")
    @Operation(summary = "通过待办事项id查询待办事项")
    public Result<Page<ToDoEventDTO>> getEventById(@PathVariable Long id){
        Page<ToDoEventDTO> toDoEventDTOPage = toDoEventDTOmapper.getEventById(new Page<>(), id);
        return Result.success(toDoEventDTOPage);
    }

    @PostMapping("/add")
    @Operation(summary = "增加待办事项")
    public Result<String> insertEvent(@RequestBody ToDoEventDTO event) {
        if (Boolean.TRUE.equals(toDoEventDTOmapper.insertEvent(event)))
            return Result.success("insert id:" + event.getId());
        try {
            throw new Exception("增加待办事项失败！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "通过id删除待办事项")
    public Result<String> deleteEventById(@PathVariable Long id){
        if (Boolean.TRUE.equals(toDoEventDTOmapper.deleteEventById(id)))
            return Result.success("delete id:" + id);
        try {
            throw new Exception("删除待办事项失败！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @PutMapping("/update")
    @Operation(summary = "通过id更新待办事项")
    public Result<String> updateEventById(@RequestBody ToDoEventDTO event){
        if (Boolean.TRUE.equals(toDoEventDTOmapper.updateEventById(event)))
            return Result.success("update id:" + event.getId());
        try {
            throw new Exception("更新待办事项失败！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
